<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" label-width="170px" label-suffix=":" v-loading="loading">
            <el-form-item label="运营" prop="admin_id">
                <el-select v-model="model.admin_id" placeholder="请选择运营" clearable filterable multiple>
                    <el-option v-for="(dict, index) in salesList" :key="index" :label="dict.admin_name" :value="dict.admin_id"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { editRoleId } from '@/api/report'
import { getOnSaleList } from '@/api/shop'
export default {
    name: 'DetailDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                id: '',
                admin_id: [],
            },
            rules: {
                admin_id: [{ required: true, message: '运营不能为空', trigger: ['blur', 'change'] }],
            },
            salesList: [],
        }
    },
    computed: {
        title() {
            return `${this.model.id ? '编辑' : '新增'}`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    const { id, admin_id } = val
                    this.model = {
                        id,
                        admin_id: admin_id ? admin_id.map(item => Number(item)) : [],
                    }
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async created() {
        this.loading = true
        try {
            const salesResult = await getOnSaleList()
            this.salesList = salesResult?.data ?? []
        } catch (error) {
            console.log(error)
        }
        this.loading = false
    },
    methods: {
        toShopPage() {
            const { shop_url } = this.model
            window.open(shop_url)
        },
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }

                const params = { ...this.model }
                if (this.save) {
                    return
                }
                this.save = true
                editRoleId(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style scoped>
.el-form-item {
    padding-right: 100px;
}
.el-form-item .el-select {
    width: 100%;
}
</style>
